/*DONE* Table [dbo].[<CompanyName>$LSC Din_ Reserv_ History Entry$5ecfc871-5d82-43f1-9c54-59685e82318d]  1/1  Script Date: 14.1.2021 - JI ******/
/*Add prestaging tables for all companies in LSInsight$Companies table*/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

DECLARE @CreateTable as VARCHAR(MAX)
DECLARE @Company as VARCHAR(100)
DECLARE @CompanyCounter as INT
DECLARE @i as INT

SET @i = 0
SET @CompanyCounter = (SELECT
		MAX([Company])
	FROM LSInsight$Companies)

WHILE @i <= @CompanyCounter 
BEGIN
SET @Company = (SELECT
		LSInsight$Companies.[CompanyPrefix]
	FROM dbo.LSInsight$Companies
	WHERE [Company] = @i)

IF @Company IS NOT NULL
BEGIN

DECLARE @DropTable as VARCHAR(MAX)
SET @DropTable = 'DROP TABLE IF EXISTS [dbo].[' + @Company + '$LSC Din_ Reserv_ History Entry$5ecfc871-5d82-43f1-9c54-59685e82318d]'
EXEC (@DropTable)

SET @CreateTable = 'CREATE TABLE [dbo].[' + @Company + '$LSC Din_ Reserv_ History Entry$5ecfc871-5d82-43f1-9c54-59685e82318d](
	[timestamp] [timestamp] NOT NULL,
	[Restaurant No_] [nvarchar](10) NOT NULL,
	[Reservation No_] [nvarchar](20) NOT NULL,
	[Client No_] [nvarchar](20) NOT NULL,
	[Guest ID_Name] [nvarchar](100) NOT NULL,
	[Date Reserved] [datetime] NOT NULL,
	[Time Reserved] [datetime] NOT NULL,
	[Reserved From Date Time] [datetime] NOT NULL,
	[Entry Type] [int] NOT NULL,
	[No_ of Guests] [int] NOT NULL,
	[Status] [int] NOT NULL,
	[Reserved for (Min_)] [decimal](38, 20) NOT NULL,
	[Reserved To Date Time] [datetime] NOT NULL,
	[Reserved To Expected Time] [tinyint] NOT NULL,
	[Registered by] [nvarchar](50) NOT NULL,
	[Expect_ Dining Duration (Min_)] [int] NOT NULL,
	[Period Dining Duration Code] [nvarchar](20) NOT NULL,
	[Dining Area ID] [nvarchar](20) NOT NULL,
	[Created Registered] [datetime] NOT NULL,
	[Arrived Registered] [datetime] NOT NULL,
	[Seated Registered] [datetime] NOT NULL,
	[Canceled Registered] [datetime] NOT NULL,
	[No-Show Registered] [datetime] NOT NULL,
	[Closed Registered] [datetime] NOT NULL,
	[Late Registered] [datetime] NOT NULL,
	[Minutes Late] [decimal](38, 20) NOT NULL,
	[Minutes Waiting for Seating] [decimal](38, 20) NOT NULL,
	[Min_ from Reserv_ to Seating] [decimal](38, 20) NOT NULL,
	[Special Requests] [nvarchar](200) NOT NULL,
	[E-Mail] [nvarchar](80) NOT NULL,
	[Phone No_] [nvarchar](30) NOT NULL,
	[Seating Cap_ of Alloc_ Tables] [int] NOT NULL,
	[No_ of Allocated Tables] [int] NOT NULL,
	[Max_ Capacity of Alloc_ Tables] [int] NOT NULL,
	[Created by Staff ID] [nvarchar](20) NOT NULL,
	[Canceled by Staff ID] [nvarchar](20) NOT NULL,
	[No-Show Reg_ by Staff ID] [nvarchar](20) NOT NULL,
	[Seated by Staff ID] [nvarchar](20) NOT NULL,
	[Arrived Reg_ by Staff ID] [nvarchar](20) NOT NULL,
	[Late Registered by Staff ID] [nvarchar](20) NOT NULL,
	[No_ Series] [nvarchar](10) NOT NULL,
	[Source Code] [nvarchar](20) NOT NULL,
	[Member Card No_] [nvarchar](100) NOT NULL,
	[Day Plan Date] [datetime] NOT NULL,
	[Period] [nvarchar](30) NOT NULL,
	[Originally Reserved From] [datetime] NOT NULL,
	[Originally Reserved To] [datetime] NOT NULL,
	[Reservation Origin] [int] NOT NULL,
	[Tables Freed] [datetime] NOT NULL,
	[History Entry Registered by] [nvarchar](50) NOT NULL,
	[Length of Stay (from Arrival)] [int] NOT NULL,
	[Length of Stay (from Reserv_)] [int] NOT NULL,
	[$systemId] [uniqueidentifier] NOT NULL,
	[Original Dining Area ID] [nvarchar](20) NOT NULL,
	[Last Changed Date Time] [datetime] NOT NULL,
	[Last Changed by Staff ID] [nvarchar](20) NOT NULL,
	[Mobile Phone No_] [nvarchar](30) NOT NULL,
	[$systemCreatedAt] [datetime] NOT NULL,
	[$systemCreatedBy] [uniqueidentifier] NOT NULL,
	[$systemModifiedAt] [datetime] NOT NULL,
	[$systemModifiedBy] [uniqueidentifier] NOT NULL,
 CONSTRAINT [' + @Company + '$LSC Din_ Reserv_ History Entry$5ecfc871-5d82-43f1-9c54-59685e82318d$Key1] PRIMARY KEY CLUSTERED 
(
	[Restaurant No_] ASC,
	[Reservation No_] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY],
 CONSTRAINT [' + @Company + '$LSC Din_ Reserv_ History Entry$5ecfc871-5d82-43f1-9c54-59685e82318d$$systemId] UNIQUE NONCLUSTERED 
(
	[$systemId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]'

EXEC(@CreateTable)

END
SET @i = @i + 1
END
